<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\AddressLogic;
use app\common\logic\ShopLogic;
use app\common\model\shop\Order;
use app\common\utils\Utils;
use think\Db;
use think\Request;


/**
 * Token接口
 */
class Niu extends Api
{
    protected $noNeedLogin = ['index', 'detail', 'search', 'getCategory', 'getGoods', 'getOrder'];
    protected $noNeedRight = '*';
    protected $shopLogic = null;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->shopLogic = new ShopLogic();
    }


    /**
     * 首页
     */
    public function index() {
        $Adszone = new \addons\adszone\library\Adszone();
        $banners = $Adszone->getAdsByMark('first_banner'); //按照标记调用广告位
        //首页3个分类
        //$cate3 = $this->shopLogic->getLimitCategory(3, ['topid'=> 0], ['is_nav' => 'desc']);

        //首页分类和3只牛 热门分类
        $cates = $this->shopLogic->getCategory(['is_hot' => 1, 'status' => 'normal']);

        foreach ($cates as $k => &$v) {
            //获取当前下面所有的商品
            $ids = $this->shopLogic->getChildIds($v['id']);
            $v['goods_items'] = $this->shopLogic->getLimitGoods(['in', $ids], 3);
        }

        $hot = $this->shopLogic->getGoodsList(['flag' => 1], 3);
        $this->success('首页数据', [
            'banners' => $banners,
            'cate3'   => [],
            'cates'   => $cates,
            'hot'     => $hot
        ]);
    }

    /**
     * 牛商品详情
     */
    public function detail() {
        $id = input('id/d', 0);
        $detail = $this->shopLogic->detail($id);
        $pics = $detail['pics'];
        $arr = Utils::explodeStr($pics);
        foreach ($arr as $k => $v) {
            $arr[$k] = cdnurl($v, true);
        }
        $detail['pics'] = Utils::implodeArr($arr);
        $detail['is_collect'] = $this->shopLogic->isCollect($id, UID);

        Db::name('shop_goods')->where('id', $id)->setInc('view_count', 1);
        $this->success('牛详情', $detail);
    }


    /**
     * 获取销售人员列表
     */
    public function getSalesMan() {
        $list = Db::name('user')
            ->where('is_yewuyuan', 1)
            ->where('status', 'normal')
            ->field(['id', 'nickname', 'username'])
            ->select();
        foreach ($list as $k => &$v) {
            $v['text'] = $v['nickname']. '_' . $v['username'] ;
        }
        $this->success('业务员', $list);
    }
    /**
     * 搜索
     */
    public function search() {
        //键为排序字段 值为asc desc 为数组
        $orders = input('orders/a', '');
        $keyword = input('keyword/s', '');
        if (!$keyword) {
            $this->success('无搜索参数', []);
        }
        $where['name'] = ['like', "%$keyword%"];
        $list = $this->shopLogic->setOrder($orders)->getGoodsList($where);

        $this->success('牛列表', $list);
    }

    /**
     * 分类
     */
    public function getCategory() {
        $list = $this->shopLogic->getTreeArray();

        $this->success('分类列表', $list);
    }


    /**
     * 获取分类下牛
     */
    public function getGoods() {
        $category_id = input('category_id/d', 0);
        $orders = input('orders/a', []); //排序条件
        $keyword = input('keyword/s', '');
        $hot = input('hot', 0);
        $where = [];
        if ($hot) {
            $where['flag'] = 1;

        }
        if ($category_id) {
            $ids = $this->shopLogic->getChildIds($category_id);
            $where['category_id'] = ['in', $ids];
        }
        if ($keyword) {
            $where['name|code'] = ['like', "%$keyword%"];
        }
        if ($orders) {
            $orderInner = $this->shopLogic->order;

            $order = array_merge($orders, $orderInner);
            $this->shopLogic->setOrder($order);
        }
        $list = $this->shopLogic->getGoodsList($where);
        $this->success('list', $list);
    }


    /**
     * 填写订单
     */
    public function order() {
        $this->shopLogic->submit();


        $this->success('提交成功');
    }

    /**
     * 我的预定
     */
    public function myReserve() {
        $type = input('type/d', 0); //0代表预定  其他代表 其他所有
        if ($type == 0) {
            $where['type'] = 0;
        } else {
            $where['type'] = ['in', [1, 2, 3]];
        }

        $where['user_id'] = UID;
        $list = $this->shopLogic->getOrders($where);
        $this->success('list', $list);
    }

    public function getAddress() {
        $address_id = input('id');
        $info = $this->shopLogic->getAddress($address_id);
        $province = $info['province'];
        $city = $info['city'];
        $area = $info['area'];
        if ($province) {
            $province = Db::name('area')->where(['id'=> $province])->find();
            $info['province_extra'] = ['name'=> $province['name'], 'code'=> $province['extra']];
        }
        if ($city) {
            $city = Db::name('area')->where(['id'=> $city])->find();
            $info['city_extra'] = ['name'=> $city['name'], 'code'=> $city['extra']];
        }
        if ($area) {
            $area = Db::name('area')->where(['id'=> $area])->find();
            $info['area_extra'] = ['name'=> $area['name'], 'code'=> $area['extra']];
        }

        $this->success('地址', $info);
    }

    public function collect() {
        $id = input('id/d', 0);
        $this->shopLogic->collect($id, UID);
        $this->success('收藏成功');
    }

    /**
     * 删除订单
     */
    public function del() {
        $id = input('id/d', 0);
        $this->shopLogic->del($id);

        $this->success('删除成功');
    }

    /**
     * 订单详情
     */
    public function orderDetail() {
        $id = input('id/d', 0);
        $data = $this->shopLogic->orderDetail($id);

        $this->success('订单信息', $data);
    }

    /**
     * 获取预定信息
     */
    public function getOrderDetail() {
        $id = input('id/d', 0);
        $type = input('type/d', 0); //0为提交订单 ， 1为修改订单
        $first = input('first/d', 1); //0为提交订单 ， 1为修改订单
        if (!$id) {
            $this->error('商品id');
        }

        if ($type == 1) {
            //$id 为订单id
            $order = Order::get($id); //订单id
            if ($first == 2) {
                $info = (new AddressLogic())->getDefault();
            } else {
                $info = $order;
                $info['id'] = $info['address_id'];
            }
            $company_text = $order['company_text'];
            $detail = $this->shopLogic->detail($order['goods_id']);
            $num = $order['num'];
        } else {
            $num = 1;
            $info = (new AddressLogic())->getDefault();
            $detail = $this->shopLogic->detail($id);
            //获取牛场名称
            $company_text = \app\common\model\custom\Niu::where(['user_id' => UID])->value('name');
        }

        if (!$detail) {
            $this->error('获取牛失败');
        }

        $company_text ? $company_text : '';
        $this->success('订单信息', [
            'company_text'=> $company_text,
            'address' => $info,
            'detail'  => $detail,
            'num' => $num
        ]);
    }

    /**排序信息*/
    public function getOrder() {
        $order = [
            ['field' =>'no', 'dec' => '不限', 'order' => 'desc'],
            ['field' =>'gtpi', 'dec' => 'GTPI 从高到低', 'order' => 'desc'],
            ['field' =>'real_money', 'dec' => '净效益指数 从高到底', 'order' => 'desc'],
            ['field' =>'milk_pro', 'dec' => '产奶育种值 从高到底', 'order' => 'desc'],
            ['field' =>'breast', 'dec' => '乳房指数 从高到底', 'order' => 'desc'],
            ['field' =>'foot', 'dec' => '肢蹄指数 从高到底', 'order' => 'desc'],
            ['field' =>'beast', 'dec' => '公牛配种难产率 从低到高', 'order' => 'asc'],
            ['field' =>'year', 'dec' => '生产寿命 从高到低', 'order' => 'desc'],
            ['field' =>'birth', 'dec' => '女儿妊娠率 从高到底', 'order' => 'desc'],
        ];
        $this->success('排序信新', [
            'order' => $order
        ]);
    }
}
